/*
 * 创建日期 2005-10-7
 *
 * 更改所生成文件模板为
 * 窗口 > 首选项 > Java > 代码生成 > 代码和注释
 */
package com.vanda.report.datas;

import java.beans.PropertyDescriptor;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.validator.GenericValidator;

import com.vanda.data.BeanListBase;
import com.vanda.db.DBFactory;
import com.vanda.db.XMLAccessor;
import com.vanda.report.ReportException;
import com.vanda.report.ReportInnerData;
import com.vanda.report.Validation;
import com.vanda.report.element.BeanElement;


/**
 * 查询指定的数据库数据，并将数据保存到指定的属性 <br>
 * <p>
 * @author eagle <br>
 * @version Version 1.00 <br>
 */
public class DataElement extends BeanElement {
	/** 数据库名称，如果不指定，则使用默认数据库 */
	private String accessor_type;
	/** SQL名称*/
	private String sql_ref;
	/** 存放查询结果属性名称*/
	private String result_property;
	
	public void validator() throws ReportException{
		Validation.checkRequired("DataElement","sql_ref",sql_ref);
		Validation.checkRequired("DataElement","result_property",result_property);
	}
	
	/**
	 * 标签执行方法，递归执行
	 * @param obj
	 */
	public void execute(Object obj) throws ReportException{
		ReportInnerData reportData = (ReportInnerData)obj;
		String property = super.getProperty();
		XMLAccessor accessor = null;
		
		try{
			//取得上下文数据
			Object contextObj = super.getNameObject(reportData);
			if(GenericValidator.isBlankOrNull(accessor_type)){
				accessor_type = com.vanda.db.cfg.Environment.HIBERNATE_DEFAULT;
			}
			//因为报表List做插入比较多,用LinkedList性能比ArrayList好
			//accessor = DBFactory.getAccessor(accessor_type,"java.util.LinkedList");
			accessor = DBFactory.getXMLAccessor();
			
			if(accessor == null){
				throw new ReportException("DBAccessor is null !");
			}
			
			Object selectData = null;
			if(GenericValidator.isBlankOrNull(property)){
				if(contextObj instanceof ReportInnerData){
					selectData = reportData.getSelectData();
				}else{
					selectData = contextObj;
				}
				
			}else{
				selectData = super.getProperty(contextObj,property);
			}
			
			Object resultObj = null;
			if(isList(contextObj,result_property)){
				BeanListBase beanList = (BeanListBase)accessor.operate(sql_ref,selectData);
				resultObj = beanList.getReferenceObject();
			}else{
				BeanListBase beanList = (BeanListBase)accessor.operate(sql_ref,selectData);
				List objList = beanList.getReferenceObject();
				if(objList != null && objList.size() > 0){
					resultObj = objList.get(0);
				}
				
			}
			
			
			if(GenericValidator.isBlankOrNull(result_property)){
				property = "reportList";
			}
			//reportData.setReportList(beanList.getReferenceObject());
			super.setProperty(contextObj,result_property,resultObj);
		}catch(ReportException e){
			throw e;
		}catch(Exception e){
			throw new ReportException(e);
		}

	}
	
	/**
	 * 检查obj下的property属性是不是List类型
	 * @param obj
	 * @param property
	 * @return
	 */
	private static boolean isList(Object obj,String property) throws Exception{
		boolean bList = false;
		try{
			if(obj instanceof Map){
				return true;
			}
			PropertyDescriptor desc = PropertyUtils.getPropertyDescriptor(obj,property);
		
			Class cls = desc.getPropertyType();
		
			if(cls.getName().equals("java.util.List")){
				return true;
			}
			Object objList = cls.newInstance();
			bList = (objList instanceof List);
		}catch(Exception e){
			e.printStackTrace();
		}

		
		return bList ;
		
	}
	
	
	
	/**
	 * @return
	 */
	public String getAccessor_type() {
		return accessor_type;
	}

	/**
	 * @return
	 */
	public String getSql_ref() {
		return sql_ref;
	}

	/**
	 * @param string
	 */
	public void setAccessor_type(String string) {
		accessor_type = string;
	}

	/**
	 * @param string
	 */
	public void setSql_ref(String string) {
		sql_ref = string;
	}

	/**
	 * @return
	 */
	public String getResult_property() {
		return result_property;
	}

	/**
	 * @param string
	 */
	public void setResult_property(String string) {
		result_property = string;
	}

	/*public static void main(String[] args){
		
		com.huawei.cfi.contract.sale.SaleContractValueObject contract = new com.huawei.cfi.contract.sale.SaleContractValueObject();
		
		try{
			System.out.println(isList(contract,"amounts"));
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}*/
}
